/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ing.sisval.gui.control;

import javax.swing.JOptionPane;
import org.ing.sisval.be.BEEspecificacionAsignada;
import org.ing.sisval.be.BEEspecificacionTecnica;
import org.ing.sisval.bl.BLEspecificacionTecnica;
import org.ing.sisval.gui.JFPrincipal;
import org.ing.sisval.gui.control.model.ModeloEspecificacionTecnica;
import org.rx.cr.gui.JInternalFrameUC;
import static org.rx.cr.util.Utilitarios.addEnterDoclickEvent;
import static org.rx.cr.util.Utilitarios.alinearDatosColumnaTablaCentro;
import static org.rx.cr.util.Utilitarios.disableComponent;
import static org.rx.cr.util.Utilitarios.enableComponent;
import static org.rx.cr.util.Utilitarios.formateaAnchoColumnaTabla;
import static org.rx.cr.util.Utilitarios.isDatosLlenos;
import static org.rx.cr.util.Utilitarios.resetComponents;

/**
 *
 * @author Mysham
 */
public class JIFEspecificacionTecnica extends JInternalFrameUC {

    private JFPrincipal parent;

    BEEspecificacionTecnica be;
    BLEspecificacionTecnica bl;
    ModeloEspecificacionTecnica model;

    /**
     * Creates new form JIFEspecificacionTecnica
     */
    public JIFEspecificacionTecnica(JFPrincipal parent) {
        initComponents();
        this.parent = parent;
        model = new ModeloEspecificacionTecnica();
        jtEspecificacionesTecnicas.setModel(model);

        bl = new BLEspecificacionTecnica(parent.getConf());
        personalizaVistaTabla();
        try {
            _listarBEs();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Error al listar la lista de valores complementarios." + e, "Atención.", JOptionPane.INFORMATION_MESSAGE);

        }
        _init();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txfTipo = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txfDescripcion = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        txfBuscar = new javax.swing.JTextField();
        jbBuscar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtEspecificacionesTecnicas = new javax.swing.JTable();
        jbCancelar = new javax.swing.JButton();
        jbGuardar = new javax.swing.JButton();
        jbEditar = new javax.swing.JButton();
        jbSalir = new javax.swing.JButton();
        jbAgregar = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setTitle("ADMINISTRACIÓN DE ESPECIFICACIONES TÉCNICAS");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "ESPECIFICACIONES TÉCNICAS", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION));

        jLabel1.setText("Tipo :");

        jLabel2.setText("Descripción :");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txfTipo, javax.swing.GroupLayout.DEFAULT_SIZE, 316, Short.MAX_VALUE)
                    .addComponent(txfDescripcion)))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txfTipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txfDescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jbBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/ing/sisval/imagen/small_buscar.png"))); // NOI18N

        jtEspecificacionesTecnicas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jtEspecificacionesTecnicas);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(txfBuscar, javax.swing.GroupLayout.DEFAULT_SIZE, 379, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbBuscar))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbBuscar)
                    .addComponent(txfBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(88, 88, 88))
        );

        jbCancelar.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jbCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/ing/sisval/imagen/Cancelar_Min.png"))); // NOI18N
        jbCancelar.setText("CANCEL");
        jbCancelar.setEnabled(false);
        jbCancelar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbCancelar.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        jbCancelar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        jbGuardar.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jbGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/ing/sisval/imagen/Guardar_Min.png"))); // NOI18N
        jbGuardar.setText("GUARDAR");
        jbGuardar.setEnabled(false);
        jbGuardar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbGuardar.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        jbGuardar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        jbEditar.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jbEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/ing/sisval/imagen/big_modificar.png"))); // NOI18N
        jbEditar.setText("EDITAR");
        jbEditar.setEnabled(false);
        jbEditar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbEditar.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        jbEditar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        jbSalir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/ing/sisval/imagen/exit_min.png"))); // NOI18N
        jbSalir.setText("SALIR");
        jbSalir.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbSalir.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        jbSalir.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jbSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbSalirActionPerformed(evt);
            }
        });

        jbAgregar.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jbAgregar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/ing/sisval/imagen/Nuevo.png"))); // NOI18N
        jbAgregar.setText("NUEVO");
        jbAgregar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbAgregar.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        jbAgregar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jbAgregar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbEditar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbGuardar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbCancelar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbSalir, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jbCancelar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jbGuardar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jbEditar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jbAgregar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jbSalir, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbSalirActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_jbSalirActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbAgregar;
    private javax.swing.JButton jbBuscar;
    private javax.swing.JButton jbCancelar;
    private javax.swing.JButton jbEditar;
    private javax.swing.JButton jbGuardar;
    private javax.swing.JButton jbSalir;
    private javax.swing.JTable jtEspecificacionesTecnicas;
    private javax.swing.JTextField txfBuscar;
    private javax.swing.JTextField txfDescripcion;
    private javax.swing.JTextField txfTipo;
    // End of variables declaration//GEN-END:variables

    @Override
    public void _cargarCombos() throws Exception {
    }

    @Override
    public void _agregaValidadoresControl() {
        addEnterDoclickEvent(new Object[][]{{txfBuscar, jbBuscar}});
        _addOperationEventsButtons(jbAgregar, jbEditar, jbGuardar, jbCancelar);
        _addRestoreDataEventsTable(jtEspecificacionesTecnicas);
        _addSearchEventsButton(jbBuscar);
    }

    @Override
    public boolean _isDatosValidosBE() {
        return isDatosLlenos(new Object[][]{
            {txfDescripcion, "Ingresar descripcion de la obra."},
            {txfTipo, "Ingresar descripcion del componente."},});
    }

    @Override

    public int _registrarBE() throws Exception {
        be = new BEEspecificacionTecnica();
        be.setTipo(txfTipo.getText());
        be.setDescripcion(txfDescripcion.getText());
        int id = bl.registrar(be);
        _resetControls();
        return id;
    }

    @Override
    public int _guardarCambiosBE() throws Exception {
        be.setTipo(txfTipo.getText());
        be.setDescripcion(txfDescripcion.getText());
        int rsp = bl.actualizarRegistro(be);

        _deshabilitarControlesDatos();
        _resetControls();
        _listarBEs();
        return rsp;
    }

    @Override
    public void _listarBEs() throws Exception {
        model.addAll(bl.buscarReferencia(txfBuscar.getText()));
        personalizaVistaTabla();
    }

    @Override
    public void _recuperarDatosBE() throws Exception {
        if (jtEspecificacionesTecnicas.getSelectedRow() >= 0) {
            be = model.get(jtEspecificacionesTecnicas.getSelectedRow());
            txfTipo.setText(be.getTipo());
            txfDescripcion.setText(be.getDescripcion());
            jbEditar.setEnabled(true);

        }
    }

    @Override
    public void _deshabilitarControlesDatos() {
        disableComponent(new Object[]{txfDescripcion, txfTipo});
    }

    @Override
    public void _habilitarControlesDatos() {
        enableComponent(new Object[]{txfDescripcion, txfTipo});
    }

    @Override
    public void _resetControls() {
        resetComponents(new Object[]{txfDescripcion, txfTipo});
    }

    private void personalizaVistaTabla() {
        alinearDatosColumnaTablaCentro(0, jtEspecificacionesTecnicas);
        alinearDatosColumnaTablaCentro(1, jtEspecificacionesTecnicas);

        formateaAnchoColumnaTabla(0, jtEspecificacionesTecnicas, 200);
        formateaAnchoColumnaTabla(1, jtEspecificacionesTecnicas, 300);

    }
}
